package com.qf.controller;

import com.qf.config.RabbitMQConfig;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;

/**
 * @author lixu
 */
@RestController
@RequestMapping("/message")
public class SendMessageController {

    @Autowired
    private RabbitTemplate rabbitTemplate;//生产者

    @GetMapping("/send")
    public String sendMessage(){
        //发消息
        rabbitTemplate.convertAndSend(
                RabbitMQConfig.EXCHANGE_TOPIC,"a.b.rabbit.a.b",
                "本次222测试的Java配置类创建交换机、队列、绑定");
        return "OK";
    }
    @GetMapping("/sendc")
    public String sendMessagec(){
        //发消息
        rabbitTemplate.convertAndSend(
                "exchange_c","c",
                "消费者测试手动应答");
        return "OK";
    }

    /**
     * 防止消费被重复消费
     *
     *
     *
     * @return
     */
    @GetMapping("/sendr")
    public String sendr(){

        //唯一
        String id = UUID.randomUUID().toString();

        CorrelationData correlationData = new CorrelationData();
        correlationData.setId(id);

        //发消息
        rabbitTemplate.convertAndSend(
                "exchange_c","c",
                "防止消费被重复消费",correlationData);
        return "OK";
    }
}
